package co.gov.medellin.ssti.sub.paoyer.administration.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.vo.AreaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.AdministrationMngr;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserGroupVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;
import co.gov.medellin.ssti.sub.service.SecretariaService;

public class ReporteUsuariosAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		SecretariaService secretariaService;
		Collection<AreaVO> areas = null;
		HttpSession session = request.getSession();

		try {
			secretariaService = new SecretariaService();
			areas = secretariaService.findSecretarias();
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}
		if (areas.size() > 0) {
			request.setAttribute("areas", areas);

		}
		AdministrationMngr delegate = new AdministrationMngr();

		UserGroupVO userGroup = new UserGroupVO();
		UserVO user1 = (UserVO) session.getAttribute("userInfo");
		request.removeAttribute("msg");
		request.setAttribute("msg", "");

		userGroup.setCode(new Long(0));
		userGroup.setWebUserModif("false");
		userGroup.setName(user1.getUserGroupName());
		Collection<UserGroupVO> userGroups = delegate
				.findUserGroupByCriterea(userGroup);
		if (userGroups != null && userGroups.size() > 0) {
			request.setAttribute("userGroups", userGroups);
		} else {
			request.setAttribute("userGroups", null);
		}

		return mapping.findForward("success");
	}
}
